package RevolCraft.TileEntities;

import RevolCraft.Interfaces.IElectric;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.FurnaceRecipes;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.TileEntity;

public class TileEntityElectricFurnace extends TileEntity implements IInventory, IElectric {
	private ItemStack[] inv;
	public int maxStorage;
	public int currentStorage;
	public int cookTime;
	
	public TileEntityElectricFurnace()
	{
		inv = new ItemStack[3];
		maxStorage = 1000;
		currentStorage = 0;
		cookTime = 0;
	}
	
	public void updateEntity()
	{
		if(currentStorage < maxStorage)
		{
			updateStorage();
		}
		if(inv[0] != null)
		{
			ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(inv[0]);
			if(inv[2] == null || result.isItemEqual(inv[2]))
			{
				int count = result.stackSize;
				if(inv[2] != null)
					count += inv[2].stackSize;
				if(count <= this.getInventoryStackLimit() && count <= result.getMaxStackSize())
				{
					if(currentStorage >= 3)
					{
						if(cookTime == 200)
						{
							currentStorage -= 3;
							cookTime = 0;
							smeltItem();
						}
						else
						{
							currentStorage -= 3;
							++cookTime;
						}
					}
				}
			}
		}
	}
	
	private void updateStorage()
	{
		TileEntity tileEntity;
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord+1, yCoord, zCoord);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord-1, yCoord, zCoord);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord, yCoord+1, zCoord);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord, yCoord-1, zCoord);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord, yCoord, zCoord+1);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
		
		tileEntity = this.worldObj.getBlockTileEntity(xCoord, yCoord, zCoord-1);
		if(tileEntity instanceof IElectric)
			takeElectrocity((IElectric)tileEntity);
	}
	
	private void takeElectrocity(IElectric source)
	{
		if(source.isSource())
		{
			if((currentStorage + 50) < maxStorage)
				currentStorage += source.takeAmount(50);
			else
				currentStorage += source.takeAmount(maxStorage-currentStorage);
		}
	}
	
	public void smeltItem()
    {
        ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(this.inv[0]);

        if (this.inv[2] == null)
        {
            this.inv[2] = result.copy();
        }
        else if (this.inv[2].isItemEqual(result))
        {
            inv[2].stackSize += result.stackSize;
        }

        --this.inv[0].stackSize;

        if (this.inv[0].stackSize <= 0)
        {
            this.inv[0] = null;
        }
    }

	@Override
	public int getSizeInventory() {
		return inv.length;
	}

	@Override
	public ItemStack getStackInSlot(int slot) {
		return inv[slot];
	}

	@Override
	public ItemStack decrStackSize(int slot, int amount) {
		ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
                if (stack.stackSize <= amount) {
                        setInventorySlotContents(slot, null);
                } else {
                        stack = stack.splitStack(amount);
                        if (stack.stackSize == 0) {
                                setInventorySlotContents(slot, null);
                        }
                }
        }
        return stack;
	}

	@Override
    public ItemStack getStackInSlotOnClosing(int slot) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                    setInventorySlotContents(slot, null);
            }
            return stack;
    }

	@Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
            inv[slot] = stack;
            if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                    stack.stackSize = getInventoryStackLimit();
            }               
    }

	@Override
	public String getInvName() {
		return "Electric Furnace";
	}

	@Override
	public int getInventoryStackLimit() {
		return 64;
	}

	@Override
    public boolean isUseableByPlayer(EntityPlayer player) {
            return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
            player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

	@Override
	public void openChest() {
	}

	@Override
	public void closeChest() {
	}
	
	public void writeToNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.writeToNBT(par1NBTTagCompound);
		NBTTagList lista = new NBTTagList();
		for(int it = 0; it < this.inv.length; it++)
		{
			if (this.inv[it] != null)
            {
				NBTTagCompound przedmiot = new NBTTagCompound();
				przedmiot.setByte("Slot", (byte)it);
				this.inv[it].writeToNBT(przedmiot);
				lista.appendTag(przedmiot);
            }
		}
		
		par1NBTTagCompound.setTag("Items", lista);
		
		par1NBTTagCompound.setInteger("maxStorage", maxStorage);
		par1NBTTagCompound.setInteger("currentStorage", currentStorage);
		par1NBTTagCompound.setInteger("cookTime", cookTime);
	}
	
	public void readFromNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.readFromNBT(par1NBTTagCompound);
        NBTTagList lista = par1NBTTagCompound.getTagList("Items");
        this.inv = new ItemStack[this.getSizeInventory()];

        for (int it = 0; it < lista.tagCount(); ++it)
        {
            NBTTagCompound przedmiot = (NBTTagCompound)lista.tagAt(it);
            int slot = przedmiot.getByte("Slot") & 255;

            if (slot >= 0 && slot < this.inv.length)
            {
                this.inv[slot] = ItemStack.loadItemStackFromNBT(przedmiot);
            }
        }
        
        maxStorage = par1NBTTagCompound.getInteger("maxStorage");
        currentStorage = par1NBTTagCompound.getInteger("currentStorage");
        cookTime = par1NBTTagCompound.getInteger("cookTime");
	}
	
	@Override
	public int getMaxStorage() {
		return maxStorage;
	}

	@Override
	public int getStorageLevel() {
		return currentStorage;
	}

	@Override
	public int takeAmount(int amount) {
		return 0;
	}

	@Override
	public boolean isSource() {
		return false;
	}
	
	public int getStorageLevelScaled(int scale)
	{
		return currentStorage * scale / maxStorage;
	}
	
	public int getSmeltLevelScaled(int scale)
	{
		return cookTime * scale / 200;
	}

}
